<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, java.util.*" %>    
<%@page import="markan.*"%>
<%
	final int rowRange  = 10; // 결과목록에 보여지는 레코드수
	final int pageRange = 10; // 페이지출력 범위 
	
	int currentPage = TCStringUtil.string2Integer(request.getParameter("pageIndex"), 1); // 현재페이지
	
	int totalCnt  = 0; // 전체 레코드수
	int firstRow = (currentPage-1) * rowRange;
	int endRow = firstRow + rowRange;

	List resultList = new ArrayList();
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	try {
		conn = new markan.db.TCConnection().getConnection();
		
		String sql = "";
		
		sql =	"SELECT COUNT(FACEBOOK_ID) FROM FACEBOOK_USER WHERE USE_AT = 'Y'";
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		if(rs.next()) {
			totalCnt = rs.getInt(1);
		}
		rs.close();
		pstmt.close();
		
		sql =	"SELECT FACEBOOK_ID, FACEBOOK_EMAIL, date_format(FRST_REGIST_PNTTM,'%Y-%m-%d') as FRST_REGIST_PNTTM "
				+ "FROM FACEBOOK_USER WHERE USE_AT = 'Y' "
				+ "ORDER BY FACEBOOK_ID DESC "
				+ "LIMIT "+firstRow+", "+endRow+"";
				//+ "LIMIT ?, ?";
		//out.println(sql);
		pstmt = conn.prepareStatement(sql);
		//pstmt.setInt(1, firstRow);
		//pstmt.setInt(2, endRow);
		rs = pstmt.executeQuery();
		while(rs.next()) {
			String FACEBOOK_ID = rs.getString("FACEBOOK_ID");
			String FACEBOOK_EMAIL = rs.getString("FACEBOOK_EMAIL");
			String FRST_REGIST_PNTTM = rs.getString("FRST_REGIST_PNTTM"); 
			
			Map map = new HashMap();
			map.put("FACEBOOK_ID", FACEBOOK_ID);
			map.put("FACEBOOK_EMAIL", FACEBOOK_EMAIL);
			map.put("FRST_REGIST_PNTTM", FRST_REGIST_PNTTM);
			resultList.add(map);
		}
	} catch(SQLException e) {
		e.printStackTrace();
	} catch(Exception e) {
		e.printStackTrace();
	} finally {
		if(rs!=null) rs.close();
		if(pstmt!=null) pstmt.close();
		if(conn!=null) conn.close();
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>페이스북 사용자 목록</title>
</head>
<body>
	총 <%=totalCnt%> 건
	<form name="frm" action="insertUser.jsp" method="post" onsubmit="return fn_save();">
		<table width="700" border="1">
			<thead>
			<tr>	
				<th width="10%">번호</th>
				<th width="70%">페이스북 이메일</th>
				<th width="20%">등록일</th>
			</tr>
			</thead>
			
			<% for(int i=0,size=resultList.size();i<size;i++) { %>
			<% Map result = (Map) resultList.get(i); %>
			<tr>
				<td align="center"><%=i+1%></td>
				<td><a href="userDetail.jsp?id=<%=result.get("FACEBOOK_ID")%>"><%=result.get("FACEBOOK_EMAIL")%></a></td>
				<td align="center"><%=result.get("FRST_REGIST_PNTTM")%></td>
			</tr>
			<% } %>
			<% if(totalCnt==0) { %>
			<tr><td colspan="3" align="center">등록된 데이터가 없습니다.</td></tr>
			<% } %>
			
		</table>
		<%=TCPagingHelper.instance.paging(totalCnt, rowRange, pageRange, currentPage)%>
		<a href="userAdd.jsp">등록</a>
	</form>
</body>
</html>
<%--
CREATE TABLE `FACEBOOK_USER` (
  `FACEBOOK_ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '페이스북 ID',
  `FACEBOOK_EMAIL` varchar(50) NOT NULL COMMENT '페이스북 로그인 이메일',
  `FACEBOOK_PASSWORD` varchar(200) DEFAULT NULL COMMENT '페이스북 로그인 암호',
  `FACEBOOK_ACCESS_TOKEN` varchar(200) DEFAULT NULL,
  `FACEBOOK_ACCESS_EXPIRES` varchar(20) DEFAULT NULL,
  `FRST_REGISTER_ID` int(11) DEFAULT NULL COMMENT '최초 등록자 ID',
  `FRST_REGIST_PNTTM` date DEFAULT NULL COMMENT '최초 등록 시점',
  `LAST_UPDUSR_ID` int(11) DEFAULT NULL COMMENT '최종 수정자 ID',
  `LAST_UPDT_PNTTM` date DEFAULT NULL COMMENT '최종 수정 시점',
  `USE_AT` char(1) DEFAULT 'Y' COMMENT '최종 수정 시점',
  PRIMARY KEY (`FACEBOOK_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='페이스북 유저';
--%>